//
//  MineViewController.m
//  Weiweiyunshi
//
//  Created by 张旭峰 on 2017/1/22.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "MineViewController.h"
#import "MineModel.h"
#import "InformationCell.h"
#import "QRCodeViewController.h"
#import "MineSetViewController.h"
#import "PersonalTableViewController.h"
#import "TestViewController.h"
#import "MeetingRoomViewController.h"
#import "LoginModel.h"
#import "LoginViewController.h"
#import "ChangePwdViewController.h"
#define URL @"https://vvuc.egoonet.com:8443/SSIL/Login.do?userId=%@&pwd=%@"
#define ID @"sunsiyuan"
#define PWD @"222222"

@interface MineViewController ()

{
    id jsonCache;
    NSString *urlStr;
    
}
@property(nonatomic,strong)MineModel * mineModel;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    urlStr = [NSString stringWithFormat:URL,ID,PWD];
    
    //通过该model取到用户数据.
    LoginModel *model = [LoginModel sharedInstance].userArr[0];
//    NSArray *dataSource = [[NSUserDefaults standardUserDefaults]objectForKey:@"loginModel"];
//    LoginModel *model = dataSource[0];
    self.mineModel = model;
    
//    [self getJsonCache];
//    [self request];
    [self setupUI];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight=UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight=150;

    [self.tableView registerNib:[UINib nibWithNibName:@"InformationCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    
    [[SDImageCache sharedImageCache]removeImageForKey:_mineModel.img];
    
    // Do any additional setup after loading the view.
}


//-(void)request{
//    [WYHttpRequest getWithURLString:urlStr parameters:nil success:^(id responseObject) {
//
//        MineModel *mineModel = [MineModel yy_modelWithJSON:responseObject];
//        self.mineModel = mineModel;
//        NSLog(@"-------->>>>>>%@",self.mineModel.userId);
//
//        [self.tableView reloadData];
//    } failure:^(NSError *error) {
//        NSLog(@"%@",error);
//    }];
//}
-(void)setupUI
{
    UITableView *tableView= [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    [self.view addSubview: tableView];
     self.tableView.delegate=self;
    self.tableView.dataSource = self;
    
    self.tableView = tableView;
    UIButton * button1 = [[UIButton alloc]init];
    
    [self.view addSubview:button1];
    button1.backgroundColor = [UIColor redColor];
//    button1.titleLabel.text = @"退出登录";
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.offset(30);
        make.left.offset(10);
        make.right.offset(-10);
        
    }];

    [button1 setTitle:@"退出登录" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(exit:) forControlEvents:UIControlEventTouchUpInside];
    [button1 setTintColor:[UIColor whiteColor]];
    button1.layer.cornerRadius=5;
    
}

-(void)exit:(id)sender{
    NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
    [defatluts removeObjectForKey:@"name"];
    [defatluts removeObjectForKey:@"password"];
    [defatluts synchronize];
    //销毁单例
    [LoginModel tearDown];
//    NSDictionary *dictionary = [defatluts dictionaryRepresentation];
//    for(NSString *key in [dictionary allKeys]){
//        [defatluts removeObjectForKey:key];
//        
//        [defatluts synchronize];
//    }
    [self presentViewController:[LoginViewController alloc] animated:YES completion:nil];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        if(indexPath.section == 0)
    {
        
        InformationCell * cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell1.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//        cell1.textLabel.text = self.mineModel.name;
      cell1.Model = self.mineModel;
        
        
//        NSString *imagePath = [NSString stringWithFormat:@"%@",_mineModel.img];
//        if(imagePath.length==0)
//        {
//        
//        [cell1.image sd_setImageWithURL:[NSURL URLWithString:self.mineModel.img]];
//        }else{
//            [[SDImageCache sharedImageCache]removeImageForKey:_mineModel.img];
            [cell1.image sd_setImageWithURL:[NSURL URLWithString:self.mineModel.img]];
        
//        NSURL *url = [NSURL URLWithString: self.mineModel.img];// 获取的图片地址
//        
//        cell1.image.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
//         cell1.name.text= self.mineModel.name;
//          cell1.mobile.text =self.mineModel.mobile;
        self.IMageView1 = cell1.image;
        return cell1;
        
    }
        else if(indexPath.section ==1)
    {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        //    cell.textLabel.text = self.mineModel.name;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.imageView.image = [UIImage imageNamed:@"QRCode2"];
        cell.textLabel.text =@"扫一扫";
        return cell;
    }
        else if (indexPath.section ==2){
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = @"我的会议室";
            cell.imageView.image = [UIImage imageNamed:@"tabbar-meetingroom-select"];
            return cell;
        }
    
        else{
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            //    cell.textLabel.text = self.mineModel.name;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.imageView.image = [UIImage imageNamed:@"setting"];
            cell.textLabel.text = @"修改密码";
            return cell;
        }
    
    
    
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    
    
    
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0 )
    {
        PersonalTableViewController * VC0 = [[PersonalTableViewController alloc]init];
        VC0.Model = self.mineModel;
        NSLog(@"-----VC0----->%@",VC0.Model);
        NSLog(@"-----self.mineModel%@",self.mineModel);
        VC0.globalImage = self.IMageView1.image;
        VC0.imageblock1 = ^(UIImage * image)
        {
            
            InformationCell * cell001= [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            [self upDateImage:image];
            cell001.image.image = image;
//            self.IMage = image;
            
        };
        
        [self.navigationController pushViewController:VC0 animated:YES];
//        [self.navigationController pushViewController:VC0 animated:YES];
        
    }
    else if(indexPath.section == 1)
    {
        QRCodeViewController * VC1 = [[QRCodeViewController alloc]init];
        [self.navigationController pushViewController:VC1 animated:YES];
    }
    else if (indexPath.section ==2){
#warning 进入我的会议室 - 待修改 -
        LoginModel *model = [LoginModel sharedInstance].userArr[0];
        
//        TestViewController *testVc = [[TestViewController alloc]init];
//        testVc.guestVRoomKey = model.vidyoKey;
//        [testVc showVidyo];
        
        MeetingRoomViewController *meetVC = [[MeetingRoomViewController alloc]init];
        [self.navigationController pushViewController:meetVC animated:YES];
    }
    else{
        [self.navigationController pushViewController:[ChangePwdViewController alloc] animated:YES];
    }
    
    
}


-(void)upDateImage:(UIImage*)photo
{
    
    NSData *data =UIImageJPEGRepresentation(photo,1.0);
    
    
    NSString *dataStr = [data base64EncodedStringWithOptions:0];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //    params[@"userId"] = @"18513255845";
    //    params[@"img"] = dataStr;
    
    LoginModel *model = [LoginModel sharedInstance].userArr[0];
    params[@"userId"] = model.userId;
    params[@"img"] = dataStr;
    [manager POST:@"http://vvuc.egoonet.com:8088/SSIL/ModifyAvatar.do"parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        
        
        
        NSLog(@"----------->>.>>>>%@",responseObject[@"Code"]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
